package parkshop.pmcourse.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import parkshop.pmcourse.domain.Customer;
import parkshop.pmcourse.domain.Orderdetail;
import parkshop.pmcourse.domain.Orders;
import parkshop.pmcourse.domain.Product;
import parkshop.pmcourse.domain.ShoppingCart;
import parkshop.pmcourse.dto.PageBean;
import parkshop.pmcourse.mapping.CustomerMapper;
import parkshop.pmcourse.mapping.OrderdetailMapper;
import parkshop.pmcourse.mapping.OrdersMapper;
import parkshop.pmcourse.mapping.ProductMapper;
import parkshop.pmcourse.mapping.ShoppingCartMapper;
import parkshop.pmcourse.service.ShoppingCartService;
import parkshop.pmcourse.utils.ConstantUtil;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	@Autowired
	private OrdersMapper orderMapper;
	@Autowired
	private OrderdetailMapper orderdetailMapper;
	
	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private CustomerMapper customerMapper;

	
	@Override
	public void saveShoppingCart(ShoppingCart shoppingCart) {
		shoppingCartMapper.insertShoppingCart(shoppingCart);
	}

	@Override
	public boolean deleteShopCartById(Long id) {
		int rows=shoppingCartMapper.deleteByPrimaryKey(id);
		return rows==1?true:false;
	}

	@Override
	public List<ShoppingCart> getShoppingCartByCustomerId(Long id) {
		// TODO Auto-generated method stub
		return shoppingCartMapper.selectShoppingCartsByCustomer(id);
	}

	@Override
	public ShoppingCart getShoppingCartByCartId(Long id) {
		return shoppingCartMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean payShoppingCart(Long id) {
		ShoppingCart cart=shoppingCartMapper.selectByPrimaryKey(id);
		Product product=productMapper.selectByPrimaryKey(cart.getProductId());
		Customer customer=customerMapper.selectByPrimaryKey(cart.getCustomerId());
		
		// save orders
		Orders order=new Orders();
		order.setOrderMoney(product.getPrice()*cart.getQuantity());
		order.setAddress(customer.getAddress());
		order.setCustomerId(customer.getId());
		order.setOrderStatus(ConstantUtil.orderStatus_unshiped);
		order.setOrderTime(new Date());
		order.setPayTime(order.getOrderTime());
		order.setShopId(product.getShopId());
		order.setCommissionRates(ConstantUtil.commissionRate);
		PageBean pageBean=new PageBean();
		pageBean.setId(cart.getCustomerId());
		pageBean.setTime(order.getOrderTime());
		
		/*
		 * 运行在aliyun 上的 centos 7 上时，
		 * orderMapper.insertSelective(order);语句将自动获得id 值。
		 * 本地运行的时候则不会
		 */
		orderMapper.insertSelective(order);
		if(order.getId()==null){
		Orders tempOrder=orderMapper.selectByCustomerIdAndOrderTime(pageBean);
		order.setId(tempOrder.getId());
		}
		// save order detail
		Orderdetail orderDetail=new Orderdetail();
		orderDetail.setProduct(product);
		orderDetail.setQuantity(cart.getQuantity());
		orderDetail.setName(product.getName());
		orderDetail.setProductId(product.getId());
		
		orderDetail.setOrderId(order.getId());
		orderdetailMapper.insertSelective(orderDetail);
		
		int rows=shoppingCartMapper.deleteByPrimaryKey(id);
		
		return rows==1?true:false;
	}

	@Override
	public List<Product> getProductsByCartList(List<ShoppingCart> cartList) {
		// TODO Auto-generated method stub
		if(cartList==null)
			return null;
		List<Product> list=new ArrayList<Product>();
		Product product=null;
		for(ShoppingCart cart:cartList){
			product=productMapper.selectByPrimaryKey(cart.getProductId());
			product.setCartId(cart.getId());
			product.setQuantity(cart.getQuantity());
			list.add(product);
		}
		
		return list;
	}

	@Override
	public PageBean getProductPageByCustomerId(Long customerId, int pageNumber) {
		// TODO Auto-generated method stub
		PageBean pageBean=new PageBean();
		int count=shoppingCartMapper.selectCountByCustomer(customerId);
		pageBean.setId(customerId);
		pageBean.setPageSize(ConstantUtil.pageSize);
		pageBean.setStartindex((pageNumber-1)*ConstantUtil.pageSize);
		List<ShoppingCart> list=shoppingCartMapper.selectPageShoppingCartsByCustomer(pageBean);
		List<Product> productList=getProductsByCartList(list);
		pageBean.setRecordList(productList);
		pageBean.setCurrentPage(pageNumber);
		pageBean.setRecordCount(count);
		pageBean.setPageCount(count%ConstantUtil.pageSize==0?count/ConstantUtil.pageSize:count/ConstantUtil.pageSize+1);
		return pageBean;
	}


}
